package com.demo2do.fms.entity.supplier.common;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;

/**
 * 服务供方业务信息
 * 
 * 与supplier关联关系:一对一
 * 
 * Created by zhonghui on 2017/7/11.
 */
@Entity
@Table(name = "FMS_SUPPLIER_CERTIFICATE")
@DynamicUpdate
public class SupplierCertificate {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @SequenceGenerator(name = "generator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;

    /** 
     * 与supplier关联 
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    private Supplier supplier;

    /**
     * 管理人员人数
     */
    private Long managerNum;

    /**
     * 具体作业人员人数
     */
    private Long operatorNum;

    /**
     * 公司规模
     * 仓储：01--大于10千万   02--5-10千万
     * 03--2-5千万  04--1-2千万
     * 05--0.5-1千万  06--0.2-0.5千万
     * 07--小于0.2千万
     * 
     * 运输：01-大于10亿
     * 02-5到10亿
     * 03-2到5亿 04-1到2亿
     * 05-5千万到1亿
     * 06-2千万到5千万
     * 07-小于2千万
     * 
     * 枚举：companySize
     */
    private String companySize;

    /**
     * 固定成本
     */
    private BigDecimal constantCost;

    /**
     * 变动成本
     */
    private BigDecimal variableCost;

    /**
     * 公司简介上传
     */
    private String briefCert;

    /**
     * 房产证扫描证上传
     */
    private String propertyCert;

    /**
     * 申请方近三年的审计报告上传
     */
    private String auditCert;

    /**
     * 申请方近三年的所得税表上传
     */
    private String taxCert;

    /**
     * 银行资信等级证明上传
     */
    private String financialCert;

    /**
     * 创建时间
     */
    @Column(name = "CREATE_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 创建者
     */
    @Column(name = "CREATOR")
    private Long creator;

    /**
     * 最近修改时间
     */
    @Column(name = " LAST_MODIFY_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastModifyTime;

    /**
     * 最近修改者
     */
    @Column(name = "LAST_OPERATOR")
    private Long lastOperator;

    @Column(name = "IS_DISABLED")
    private Boolean disabled;
    
    /**
     * Instantiates a new Supplier info.
     */
    public SupplierCertificate() {
    }
    
    /**
     * 
     * @param supplier
     */
    public SupplierCertificate(Supplier supplier, Long operator) {
        this.supplier = supplier;
        this.creator = operator;
        this.createTime = new Date();
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        this.disabled = false;
    }
    
    /**
     * @param operator
     * @return
     */
    public SupplierCertificate edit(Long operator) {
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        this.disabled = false;
        return this;
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isValid() {
        return (this.managerNum != null) &&
               (this.operatorNum != null) &&
               StringUtils.isNoneBlank(this.companySize) &&
               (this.constantCost != null) &&
               (this.variableCost != null) &&
               StringUtils.isNoneBlank(this.briefCert) &&
               StringUtils.isNoneBlank(this.propertyCert) &&
               StringUtils.isNoneBlank(this.taxCert) &&
               (this.disabled == false);
    }
    
    /**
     * @return
     */
    public String inValidMessage() {
        if (managerNum == null) {
            return "管理人员人数不存在";
        } else if (operatorNum == null) {
            return "具体作业人员人数不存在";
        } else if (StringUtils.isBlank(companySize)) {
            return "公司规模信息不存在";
        } else if (this.constantCost == null || this.variableCost == null) {
            return "固定成本或变动成本信息不存在";
        } else if (StringUtils.isBlank(briefCert)) {
            return "公司简介未上传";
        } else if (disabled) {
            return "无效服务供方业务信息";
        } else {
            return null;
        }
    }
    
    /**
     * @return
     */
    public String getCompanySizeString() {
        
        CompanySize size = CompanySize.codeOf(this.companySize);
        if (size == null) {
            return null;
        }
        
        if (supplier.isMotor()) {
            return size.getTransDescription();
        } else {
            return size.getWareDescription();
        }
        
    }
    
    /**
     * @param operator
     * @param now
     * @return
     */
    public SupplierCertificate disable(Long operator) {
        this.disabled = true;
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the supplier
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * @return the managerNum
     */
    public Long getManagerNum() {
        return managerNum;
    }

    /**
     * @return the operatorNum
     */
    public Long getOperatorNum() {
        return operatorNum;
    }

    /**
     * @return the companySize
     */
    public String getCompanySize() {
        return companySize;
    }

    /**
     * @return the constantCost
     */
    public BigDecimal getConstantCost() {
        return constantCost;
    }

    /**
     * @return the variableCost
     */
    public BigDecimal getVariableCost() {
        return variableCost;
    }

    /**
     * @return the briefCert
     */
    public String getBriefCert() {
        return briefCert;
    }

    /**
     * @return the propertyCert
     */
    public String getPropertyCert() {
        return propertyCert;
    }

    /**
     * @return the auditCert
     */
    public String getAuditCert() {
        return auditCert;
    }

    /**
     * @return the taxCert
     */
    public String getTaxCert() {
        return taxCert;
    }

    /**
     * @return the financialCert
     */
    public String getFinancialCert() {
        return financialCert;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the creator
     */
    public Long getCreator() {
        return creator;
    }

    /**
     * @return the lastModifyTime
     */
    public Date getLastModifyTime() {
        return lastModifyTime;
    }

    /**
     * @return the lastOperator
     */
    public Long getLastOperator() {
        return lastOperator;
    }

    /**
     * @return the disabled
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param managerNum the managerNum to set
     */
    public void setManagerNum(Long managerNum) {
        this.managerNum = managerNum;
    }

    /**
     * @param operatorNum the operatorNum to set
     */
    public void setOperatorNum(Long operatorNum) {
        this.operatorNum = operatorNum;
    }

    /**
     * @param companySize the companySize to set
     */
    public void setCompanySize(String companySize) {
        this.companySize = companySize;
    }

    /**
     * @param constantCost the constantCost to set
     */
    public void setConstantCost(BigDecimal constantCost) {
        this.constantCost = constantCost;
    }

    /**
     * @param variableCost the variableCost to set
     */
    public void setVariableCost(BigDecimal variableCost) {
        this.variableCost = variableCost;
    }

    /**
     * @param briefCert the briefCert to set
     */
    public void setBriefCert(String briefCert) {
        this.briefCert = briefCert;
    }

    /**
     * @param propertyCert the propertyCert to set
     */
    public void setPropertyCert(String propertyCert) {
        this.propertyCert = propertyCert;
    }

    /**
     * @param auditCert the auditCert to set
     */
    public void setAuditCert(String auditCert) {
        this.auditCert = auditCert;
    }

    /**
     * @param taxCert the taxCert to set
     */
    public void setTaxCert(String taxCert) {
        this.taxCert = taxCert;
    }

    /**
     * @param financialCert the financialCert to set
     */
    public void setFinancialCert(String financialCert) {
        this.financialCert = financialCert;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param creator the creator to set
     */
    public void setCreator(Long creator) {
        this.creator = creator;
    }

    /**
     * @param lastModifyTime the lastModifyTime to set
     */
    public void setLastModifyTime(Date lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    /**
     * @param lastOperator the lastOperator to set
     */
    public void setLastOperator(Long lastOperator) {
        this.lastOperator = lastOperator;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

}
